﻿<?php


class NavApp extends BaseApp
{
    function index()
    {

		$TB_nav = &$this->_model('nav'); 	 


		$navdata = $this->get_list_category($TB_nav->show());
		
				
        $this->assign('navdata',$navdata);	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('nav_list.view.php');
    }

    function edit()
    {	
	    $TB_alonepage = &$this->_model('alonepage');		
		$data = $TB_alonepage->show();	
        $this->assign('lw_data', $data['data']);
	    $TB_alonepage = &$this->_model('subject');		
		$data = $TB_alonepage->show();	
        $this->assign('subject_data', $data['data']);		
		
        $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();
        $module_cat_data = array();
		foreach($module_list_data as $key => $value)
		{
		   $module_cat_data[$key]['alias']= $value['alias'];
		   $module_cat_data[$key]['id']= $value['id'] ;
		   $module_cat_data[$key]['appname']= $value['appname'] ;
		   $module_cat_data[$key]['catdata'] = $GLOBALS['SCup']->_get_category_data($value['appname']);
		}
        $this->assign('module_cat_data', $module_cat_data);	
		
		$id = $_GET['id'];
		$TB_nav = &$this->_model('nav'); 	 

		$data = $TB_nav->show();		
		$navdata = $TB_nav->oneshow($id);
        $navdata['config'] =@unserialize($navdata['config']);
		$catgystr = $this->html_format_category($data,$navdata['parent_id'],0,0,$id);	
        $this->assign('catgystr', $catgystr);
	    $this->assign('navdata', $navdata);		
			
	    $this->assign('nav_lang', get_all_dirname(ROOT_PATH . '/languages'));	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('nav_edit.view.php');
    }

    function add()
    {	
	    $TB_alonepage = &$this->_model('alonepage'); 		
		$data = $TB_alonepage->show();	
        $this->assign('lw_data', $data['data']);
	    $TB_alonepage = &$this->_model('subject');		
		$data = $TB_alonepage->show();	
        $this->assign('subject_data', $data['data']);			
		
        $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();
        $module_cat_data = array();
		foreach($module_list_data as $key => $value)
		{
		   $module_cat_data[$key]['alias']= $value['alias'];
		   $module_cat_data[$key]['id']= $value['id'] ;
		   $module_cat_data[$key]['appname']= $value['appname'] ;
		   $module_cat_data[$key]['catdata'] = $GLOBALS['SCup']->_get_category_data($value['appname']);
		}
        $this->assign('module_cat_data', $module_cat_data);		

		$TB_nav = &$this->_model('nav');		
		$navdata = $TB_nav->show();
		$catgystr = $this->html_format_category($navdata,0);	

        $this->assign('catgystr', $catgystr);
			

			
	    $this->assign('nav_lang', get_all_dirname(ROOT_PATH . '/languages'));	
		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('nav_add.view.php');
    }


    function addpost()
    {

	     $data = $_POST['data'];
		 
         $data_config = $data['config'];
		 
		 unset($data['config']);
		 
		 $data['config'] = serialize($data_config);//@unserialize
		 
		 $data['scup'] = 'scup';
	 
		 $TB_nav = &$this->_model('nav'); 	 
		
		 $TB_nav->post($data);
		 
		 $this->write_cache();
		 $this->index();

    }


    function editpost()
    {

	     $data = $_POST['data'];

		 
		 $id = intval($data['id']);	 
		 
         $data_config = $data['config'];
		 
		 unset($data['config']);
		 
		 $data['config'] = serialize($data_config);//@unserialize		 

		 $data['scup'] = 'scup';
		 

		 $TB_nav = &$this->_model('nav'); 	 
		
		 $TB_nav->editaction($id,$data);
		 
		 $this->write_cache();
		 $this->index();
		 

    }

    function lsort()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['sort'] = $_GET['v'];
		 
		 $TB_nav = &$this->_model('nav'); 	 		
		 $TB_nav->editsort($id,$data); 
		 
		 $this->write_cache();		 
		 $this->index();			  

    }

    function lname()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['name'] = $_GET['v'];
		 
		 $TB_nav = &$this->_model('nav');  	 		
		 $TB_nav->editname($id,$data); 
		 $this->write_cache();		 
		 $this->index();			  

    }
	

    function del()
    {

	     $id = $_GET['id'];
		 $TB_nav = &$this->_model('nav'); 	 
			 
		 $TB_nav->del($id);	
		 	 
		 $this->write_cache();		 
		 $this->index();	  

    }




    function cache()
    {
         
		 $this->write_cache();		 
		 $this->index();		 


    }	

    function write_cache()
    {
        $dir =  ROOT_PATH . '/data/nav/';
		if(is_dir($dir)&&file_exists($dir))
		{
		     del_file($dir);	
		}			
        get_dir(ROOT_PATH . '/data/nav/');	 		 
		$TB_nav = &$this->_model('nav'); 
					
		$navdata = $TB_nav->show();
		$navdata = $this->get_list_category($navdata);
		
        $nav = & $GLOBALS['SCup']->_get_nav();	
		
		foreach($navdata as $key => $value)
		{				
        $nav->set_file($value['type'],$value['lang_type']);
				
        $nav->_set_nav($value,'sort_order');		
		}

    }

/*
      function write_cache($type='') 
	  {
	     if($type)
		 {
		  $this->V_type = $type;
		 }
		 
		$data = $this->get_list_category($this->show($this->V_type));
		  		 		  
		$cache_filename = ROOT_PATH . '/data/category/category_'. $this->V_type .'.php';
		
	    write_file($cache_filename, "<?php \nreturn " . var_export($data , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
		
     }

*/

	
      function getnbsp($i) 
	  {
	        $str = '';
	        if($i) 
			{
		         for($j=0; $j<$i; $j++)
				 {
			      $str .= "　";
		         }
	        }
	        return $str;
     }




     function get_list_category($array,$parentid=0,$level=0,$add=2) 
	 {
	        $str_pad = $this->getnbsp($level);
	        $newarray = array();
	        $temp = array();
	        foreach($array as $v) 
			{
		       if($v['parent_id'] == $parentid) 
			   {
			       $newarray[] = array(
				         'nav_id' => $v['nav_id'],
				         'name' => $v['name'],
				         'parent_id' => $v['parent_id'],
				         'sort_order' => $v['sort_order'],
				         'link' => $v['link'],
				         'whree' => $v['whree'],
					     'type' => $v['type'],
					     'lang_type' => $v['lang_type'],
					     'config' => @unserialize($v['config']),
				         'str_pad' => $str_pad
			                          );
				   
			       $temp = $this->get_list_category($array,$v['nav_id'],($level+$add));
			       if($temp) 
				   {      
				          $newarray = array_merge($newarray, $temp);
			       }


		      }
	       }

	       return $newarray;
    }


    function html_format_category($array,$value,$parentid=0,$level=0,$ownid=0) 
	{
	       $newarray = $this->get_list_category($array,$parentid=0,$level=0,$add=1);
           $str='';
	       foreach($newarray as $v) 
		   {
		            $selected = '';
		            if($value == $v['nav_id']) 
					{
					 $selected = 'selected';
					}
					if($ownid && $ownid == $v['nav_id'])
			        {
					$str .= "<optgroup label='".$v['str_pad'].$v['name']."'></optgroup>";
					}else
			       {
		            $str .= "<option value='".$v['nav_id']."' ".$selected.">".$v['str_pad'].$v['name']."</option>";
				   }
	       }
	       return $str;
    }


  
}

?>